package com.ruoyi.web.controller.system;

import java.io.IOException;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysArea;
import com.ruoyi.system.service.ISysAreaService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

import javax.servlet.http.HttpServletResponse;

/**
 * 行政区域管理Controller
 *
 * @author moweiyu
 * @date 2021-04-22
 */
@Controller
@RequestMapping("/system/area")
public class SysAreaController extends BaseController
{
    private String prefix = "system/area";

    @Autowired
    private ISysAreaService sysAreaService;

    @RequiresPermissions("system:area:view")
    @GetMapping()
    public String area()
    {
        return prefix + "/area";
    }

    /**
     * 查询行政区域管理树列表
     */
    @RequiresPermissions("system:area:list")
    @PostMapping("/list")
    @ResponseBody
    public List<SysArea> list(SysArea sysArea)
    {
        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
        return list;
    }

    /**
     * 通过 areaName 查询行政区域 Ajax （新增页面）
     */
    @RequiresPermissions("system:area:list")
    @PostMapping("/listAjax")
    @ResponseBody
    public void listAjax(HttpServletResponse response, String areaName) throws IOException {
        String res = sysAreaService.selectSysAreaByName(areaName);

        String data = null;
        if (res != null) {
            data = "{\"res\":\"fail\"}";
        } else {
            data = "{\"res\":\"success\"}";
        }

        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().write(data);
//        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
//        return list;
    }

    /**
     * 通过 areaName 查询行政区域 Ajax (更新页面)
     */
    @RequiresPermissions("system:area:list")
    @PostMapping("/listAjaxUpdate")
    @ResponseBody
    public void listAjaxUpdate(HttpServletResponse response, String originAreaName, String areaName) throws IOException {

//        System.out.println(originAreaName+"--"+areaName);
        String res = sysAreaService.selectSysAreaByName(areaName);

        String data = null;
        if (res!=null && !res.equals(originAreaName)) {
            data = "{\"res\":\"fail\"}";
        } else {
            data = "{\"res\":\"success\"}";
        }

        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().write(data);
//        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
//        return list;
    }

    /**
     * 导出行政区域管理列表
     */
    @RequiresPermissions("system:area:export")
    @Log(title = "行政区域管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysArea sysArea)
    {
        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
        ExcelUtil<SysArea> util = new ExcelUtil<SysArea>(SysArea.class);
        return util.exportExcel(list, "行政区域管理数据");
    }

    /**
     * 新增行政区域管理
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("sysArea", sysAreaService.selectSysAreaById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存行政区域管理
     */
    @RequiresPermissions("system:area:add")
    @Log(title = "行政区域管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysArea sysArea)
    {
        return toAjax(sysAreaService.insertSysArea(sysArea));
    }

    /**
     * 修改行政区域管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysArea sysArea = sysAreaService.selectSysAreaById(id);
        mmap.put("sysArea", sysArea);
        return prefix + "/edit";
    }

    /**
     * 修改保存行政区域管理
     */
    @RequiresPermissions("system:area:edit")
    @Log(title = "行政区域管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysArea sysArea)
    {
        return toAjax(sysAreaService.updateSysArea(sysArea));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:area:remove")
    @Log(title = "行政区域管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public void remove(HttpServletResponse response, Long fatherId) throws IOException {

        int res = sysAreaService.selectSysAreaByFatherId(fatherId);

        String data = null;
        if (res > 0) {
            data = "{\"res\":\"fail\"}";
        } else {
            int delRes = sysAreaService.deleteSysAreaById(fatherId);
            if (delRes > 0) {
                data = "{\"res\":\"success\"}";
            } else {
                data = "{\"res\":\"delFail\"}";
            }
        }

        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().write(data);

//        System.out.println(111);
//        return toAjax(sysAreaService.deleteSysAreaById(id));
    }

    /**
     * 选择行政区域管理树
     */
    @GetMapping(value = { "/selectAreaTree/{id}", "/selectAreaTree/" })
    public String selectAreaTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("sysArea", sysAreaService.selectSysAreaById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载行政区域管理树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = sysAreaService.selectSysAreaTree();
        return ztrees;
    }
}
